package com.seya.calculite.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class HistoryActivity extends Activity {

    DetailAdapter adapter;
    List<Details> dList;
    ListView lstView;
    DatabaseHandler dbHandler;
    Context c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
        setContentView(R.layout.activity_history);

        c = this;

        FontHelper.applyFont(this, findViewById(R.id.activity_main_root), "RobotoLight.ttf");
        dbHandler = new DatabaseHandler(HistoryActivity.this);
        dList = new ArrayList<Details>();
        dList = dbHandler.getAllDetailss();
        adapter = new DetailAdapter(dList, HistoryActivity.this, R.layout.activity_detail_item);
        lstView = (ListView) findViewById(R.id.listdetails);
        lstView.setAdapter(adapter);

        registerForContextMenu(lstView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.history, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        /*if (id == R.id.action_settings) {
            return true;
        }*/
        switch(id) {
            case R.id.action_logout:
                SharedPreferences settings = c.getSharedPreferences(SplashScreenActivity.
                        SHARED_PREFERENCES_NAME, MODE_PRIVATE);
                settings.edit().clear().commit();
                SplashScreenActivity.loggingOut = true;
                Intent i = new Intent(HistoryActivity.this, LoginActivity.class);
                startActivity(i);
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, v.getId(), 0, "Delete All");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle() == "Delete All") {
            dbHandler.deleteAllDetails();
            //Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
            HistoryActivity.this.finish();
        }
        else {
            return false;
        }
        return true;
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        //closing transition animations
        overridePendingTransition(R.anim.activity_open_scale,R.anim.activity_close_translate);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(SplashScreenActivity.loggingOut){
            finish();
        }
    }

}
